package cn.zcx;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * description:原子售票
 * author:zcx
 * lenovo
 * 时间：2022.02.08.09.06
 */
public class T27 {
    AtomicInteger num=new AtomicInteger(50);
 // static   int num=50;
  static  int n=0;
   static CountDownLatch latch=new CountDownLatch(6);
    void  seller(){
        String tn=Thread.currentThread().getName();
     while (true){
         synchronized (this) {
             if(num.get()>0) {
                 num.decrementAndGet();
                 System.out.printf("%s 售出一张票,剩余%d张票。%n", tn, num.get());
             }else{ break;}

             try {
                 Thread.sleep(50);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }

     }
    }
    public static void main(String[] args) {
        var t=new T27();
        for (int i = 0; i < 6; i++) {
            ++n;
            new Thread(t::seller,"T"+i).start();
        }
    }
}
